import Vue from 'vue'
import VueRouter from 'vue-router'
import Reg from '@/views/Reg/Reg'
import Login from '@/views/Login/Login'
import main from '@/views/main/main'
import store from '@/store'
import Home from '@/views/Menus/Home/Home'
import UserInfo from '@/views/Menus/User/UserInfo'
import UserAvatar from '@/views/Menus/User/UserAvatar'
import UserPwd from '@/views/Menus/User/UserPwd'
import ArtCate from '@/views/Menus/article/ArtCate'
Vue.use(VueRouter)

const routes = [
  { path: '/Reg', component: Reg },
  { path: '/Login', component: Login },
  {
    path: '/',
    component: main,
    redirect: '/home',
    children: [
      {

        path: 'home',
        component: Home
      },
      {

        path: 'user-info',
        component: UserInfo
      },
      {

        path: 'user-avatar',
        component: UserAvatar
      },
      {

        path: 'user-pwd',
        component: UserPwd
      },
      {

        path: 'art-cate',
        component: ArtCate
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  const token = store.state.user.token

  const whiteList = ['/login', '/reg']

  if (!token && !whiteList.includes(to.path)) {
    // 打回登录页
    next('/login')

    Vue.prototype.$message.error('您还未登陆哦!亲!')
  } else {
    next()
  }
})

export default router
